<?php

namespace Home\Controller;
use Common\Controller\BaseController;

class SystemController extends BaseController {

    public function index(){

        $condition = I('get.condition','','htmlspecialchars');
        $userModel = D('User');
        $where     = !empty($condition)  ? "locate('{$condition}',user.nickname) and del=1": "del=1";

        $field = "user.*,role.name as rolename";
        $list = $userModel
            ->field($field)
            ->join('role on user.role_id=role.role_id')
            ->where($where)
            ->order('user.reg_time')
            ->select();

        $this->assign('list',$list);
        $this->assign('condition',$condition);

        $this->display('index');
    }

    public function add(){

        $roleModel = D('Role');

        $role_list = $roleModel->getRoleList();

        $this->assign('role_list',$role_list);
        $this->assign('now',date('Y-m-d',time()));
        $this->display('add');
    }

    public function edit(){

        $user_id    = I('get.uid');
        $userModel  = D('User');

        $user = $userModel->where('user_id='.intval($user_id))->limit(1)->select();

        if(empty($user)){
            $this->error('员工不存在');
        }

        $roleModel = D('Role');
        $role_list = $roleModel->getRoleList();

        $this->assign('role_list',$role_list);
        $this->assign('user',$user[0]);
        $this->display('edit');
    }

    public function save(){

        $ret = array('status'=>200,'msg'=>'','content'=>'');

        if(IS_POST){
            try{

                $userModel = D('User');
                $post      = I('post.');
                $res = $userModel->saveUser($post);
                $ret['status'] = (false==$ret) ? 0 : 200;
                $ret['msg'] = $res['msg'];

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '请求不允许';
        }

        $this->ajaxReturn($ret);
    }
    public function delete(){

        $user_id    = I('get.uid');
        $userModel  = D('User');

        $user = $userModel->where('user_id='.intval($user_id))->limit(1)->select();

        if(!empty($user)){
            try{
                $info = array('user_id'=>intval($user_id),'del'=>2);
                $res = $userModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '用户不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $user_id    = I('get.uid');
        $userModel  = D('User');

        $field = "user.*,role.name as rolename";
        $user = $userModel
            ->field($field)
            ->join('role on user.role_id=role.role_id')
            ->where('user.user_id='.intval($user_id."and del=1"))
            ->limit(1)
            ->select();

        if(empty($user)){
            $this->error('员工不存在');
        }

        $this->assign('user',$user[0]);
        $this->display('view');
    }
}